package org.raymond.iworks.study.basic.thread.visibility.falsesharing;

import java.util.concurrent.CountDownLatch;

/**
 * @author raymond
 * @version V1.0
 * @Description: TODO
 */
public class CacheLinePadding {
    public static long COUNT = 10_0000_0000L;

    private static class T {
        private long p1, p2, p3, p4, p5, p6, p7;// p8, p9, p10, p11, p12, p13, p14, p15;
        public long x = 0L;
        private long p16, p17, p18, p19, p20, p21, p22;// p23, p24, p25, p26, p27, p28, p29, p30, p31;
    }

    public static T[] arr = new T[2];

    static {
        arr[0] = new T();
        arr[1] = new T();
    }
    public static void main(String[] args) throws InterruptedException {
        CountDownLatch latch = new CountDownLatch(2);

        Thread t1 = new Thread(()->{
            for (long i=0; i<COUNT; i++) {
                arr[0].x = i;
            }

            latch.countDown();
        });

        Thread t2 = new Thread(()->{
            for (long j=0; j<COUNT; j++) {
                arr[1].x = j;
            }

            latch.countDown();
        });

        final long start = System.nanoTime();
        t1.start();
        t2.start();
        latch.await();
        System.out.println((System.nanoTime()-start)/100_0000);
    }
}
